import cx_Oracle

# # 连接到本地Oracle数据库
# def connect_to_oracle():
#     dsn_tns = cx_Oracle.makedsn('localhost', '1521', service_name='orcl')
#     try:
#         conn = cx_Oracle.connect(user='sys', password='gule021017', dsn=dsn_tns, mode=cx_Oracle.SYSDBA)
#         print("数据库连接成功！")
#         return conn
#     except cx_Oracle.DatabaseError as e:
#         error, = e.args
#         print(f"数据库连接失败: {error.message}")
#         return None
#
# # 连接到Oracle数据库
# def connect_to_oracle():
#     dsn_tns = cx_Oracle.makedsn('10.1.161.137', '1521', service_name='orcl')
#     try:
#         conn = cx_Oracle.connect(user='sys', password='123456', dsn=dsn_tns, mode=cx_Oracle.SYSDBA)
#         print("数据库连接成功！")
#         return conn
#     except cx_Oracle.DatabaseError as e:
#         error, = e.args
#         print(f"数据库连接失败: {error.message}")
#         return None







import jaydebeapi
import jpype

# # JDBC 配置
# jdbc_url = 'jdbc:oracle:thin:@//10.1.161.137:1521/ORCL'
# driver = 'oracle.jdbc.driver.OracleDriver'
# jar_path = r'D:\Py\LLM\ojdbc6-11.2.0.3.jar'
# user = 'sys'
# password = '123456'
#
# # 启动 JVM（需在调用 Java 类之前执行）
# jpype.startJVM(
#     jpype.getDefaultJVMPath(),  # 自动检测 JVM 路径
#     "-Djava.class.path={}".format(jar_path)  # 添加 JDBC 驱动到类路径
# )
#
# try:
#     # 创建 Java Properties 对象
#     java_properties = jpype.JClass('java.util.Properties')()
#     java_properties.put('user', user)
#     java_properties.put('password', password)
#     java_properties.put('internal_logon', 'sysdba')  # 需要 SYSDBA 权限时添加
#
#     # 建立连接
#     conn = jaydebeapi.connect(
#         driver,
#         jdbc_url,
#         [java_properties],  # 传递 Properties 对象
#         jar_path
#     )
#     cursor = conn.cursor()
#     cursor.execute("SELECT * FROM BAYONET_BASICS.SYS_BAYONET")
#     print(cursor.fetchall())
#     print("连接成功")
# except Exception as e:
#     print("连接失败:", e)
# finally:
#     # 关闭连接和 JVM
#     if 'conn' in locals():
#         conn.close()
#     jpype.shutdownJVM()  # 关闭 JVM







# def connect_to_oracle():
#     # JDBC 配置（根据实际情况修改）
#     jdbc_url = 'jdbc:oracle:thin:@//10.1.161.137:1521/ORCL'
#     driver = 'oracle.jdbc.driver.OracleDriver'
#     jar_path = r'D:\Py\LLM\ojdbc6-11.2.0.3.jar'  # 确保路径正确
#     user = 'sys'
#     password = '123456'
#
#     # 启动 JVM（仅首次调用时执行）
#     if not jpype.isJVMStarted():
#         jpype.startJVM(
#             jpype.getDefaultJVMPath(),
#             "-Djava.class.path={}".format(jar_path)
#         )
#
#     try:
#         # 连接参数（含 SYSDBA 权限）
#         driver_args = {
#             'user': user,
#             'password': password,
#             'internal_logon': 'sysdba'
#         }
#
#         # 建立连接
#         conn = jaydebeapi.connect(
#             driver,
#             jdbc_url,
#             driver_args,
#             jar_path
#         )
#         print("数据库连接成功！")
#         return conn  # 返回连接对象
#
#     except Exception as e:
#         print(f"数据库连接失败: {str(e)}")
#         return None  # 连接失败返回空